package todo;

import se.lth.cs.realtime.RTThread;
import done.*;

public class WashingController implements ButtonListener {	
	AbstractWashingMachine washingMachine;
	double speed;
	TemperatureController tempController;
	WaterController waterController;
	SpinController spinController;
	WashingProgram2 wash2;
	WashingProgram1 wash1;
	WashingProgram3 wash3;
	WashingProgram0 wash0;
	RTThread runningThread;
	// TODO: add suitable attributes
	
    public WashingController(AbstractWashingMachine theMachine, double theSpeed) {
    	washingMachine = theMachine;
    	speed = theSpeed;
    	tempController = new TemperatureController(washingMachine, theSpeed);
    	waterController = new WaterController(washingMachine, theSpeed);
    	spinController = new SpinController(washingMachine, theSpeed);
    	
    	wash0 = new WashingProgram0(washingMachine, speed, tempController, waterController, spinController);
    	wash2 = new WashingProgram2(washingMachine, speed, tempController, waterController, spinController);
    	wash1 = new WashingProgram1(washingMachine, speed, tempController, waterController, spinController);
    	wash3 = new WashingProgram3(washingMachine, speed, tempController, waterController, spinController);
    	runningThread = wash1;
    	
    	tempController.start();
    	waterController.start();
    	spinController.start();
		// TODO: implement this constructor
    }

    public void processButton(int theButton) {
    	
    
		switch(theButton){
		case 0:
			if (runningThread.isAlive()){
				runningThread.interrupt();
			}
			wash0 = new WashingProgram0(washingMachine, speed, tempController, waterController, spinController);
			runningThread = wash0;
			wash0.start();
			break;
			
		case 1:
			if (runningThread.isAlive() || runningThread == wash0){
				break;
			} 
			wash1 = new WashingProgram1(washingMachine, speed, tempController, waterController, spinController);
			runningThread = wash1;
			wash1.start();
			break;
			
		case 2:
			if (runningThread.isAlive() || runningThread == wash0){
				break;
			} 
			wash2 = new WashingProgram2(washingMachine, speed, tempController, waterController, spinController);
			runningThread = wash2;
			wash2.start();
			break;
			
		case 3:
			if (runningThread.isAlive()){
				break;
			} 
			wash3 = new WashingProgram3(washingMachine, speed, tempController, waterController, spinController);
			runningThread = wash3;
			wash3.start();
			break;
			
		default:
			break;
				
		}
    }
}
